#include "countresitem.h"


CountResItem::CountResItem(int id, const std::pair<int, int> &position, float radius, QGraphicsItem *parent)
    : QGraphicsItem(parent),
    m_id(id),
    m_position(position),
    m_radius(radius)
{
    // 初始化默认样式
    setDefaultStyle();
}

QRectF CountResItem::boundingRect() const
{
    return QRectF(m_position.first  - m_radius - 2,
                  m_position.second  - m_radius - 2,
                  m_radius * 2 + 4,
                  m_radius * 2 + 4);
}

void CountResItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(option);
    Q_UNUSED(widget);

    painter->setRenderHint(QPainter::Antialiasing);

    // 绘制圆形区域
    painter->setPen(m_pen);
    painter->setBrush(m_brush);
    QRectF circleRect(m_position.first  - m_radius,
                      m_position.second  - m_radius,
                      m_radius * 2,
                      m_radius * 2);
    painter->drawEllipse(circleRect);

    // 绘制中心点
    painter->drawPoint(m_position.first,  m_position.second);

    // 显示物体编号
    painter->setPen(m_textPen);
    painter->setFont(m_font);
    painter->drawText(m_position.first  + m_radius + 5,
                      m_position.second,
                      QString::number(m_id));
}

void CountResItem::mousePressEvent(QGraphicsSceneMouseEvent* event)
{
    // 点击时高亮显示
    QPen highlightPen(Qt::yellow, 3);
    setPen(highlightPen);
    update();
    QGraphicsItem::mousePressEvent(event);
}

void CountResItem::hoverEnterEvent(QGraphicsSceneHoverEvent* event)
{
    // 鼠标悬停时显示详细信息
    setToolTip(QString("Object %1\nPosition: (%2, %3)\nRadius: %4")
                   .arg(m_id)
                   .arg(m_position.first)
                   .arg(m_position.second)
                   .arg(m_radius));
    QGraphicsItem::hoverEnterEvent(event);
}

void CountResItem::hoverLeaveEvent(QGraphicsSceneHoverEvent* event)
{
    // 恢复默认样式
    setDefaultStyle();
    update();
    QGraphicsItem::hoverLeaveEvent(event);
}
